package com.ruoyi.ommpuser.controller;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ommpuser.domain.OmMpuser;
import com.ruoyi.ommpuser.service.IOmMpuserService;
import com.ruoyi.ommpuser.wxutils.WxUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * 问诊用户Controller
 * 
 * @author ningyi
 * @date 2025-02-10
 */
@RestController
@RequestMapping("/mp/om/mpuser")
public class MpOmMpuserController extends BaseController
{
    @Autowired
    private IOmMpuserService omMpuserService;

    @Autowired
    private WxUtil wxUtil;

    //写个微信login
    @PostMapping("/getOpenid")
    public AjaxResult getOpenid(@RequestBody HashMap<String,Object> inMap) throws Exception {
         logger.info(JSON.toJSONString(inMap));
        HashMap<String,Object> result = new HashMap<>();
        String openid = wxUtil.getWxOpenId(inMap.get("code").toString());
        result.put("openid",openid);
        return success(result);
    }

    //写一个用户添加的的
    //注意头像。所以要让前端先 调一下 上传头像接口
    @PostMapping("/addMpUser")
    public AjaxResult addMpUser(@RequestBody OmMpuser omMpuser)
    {
        logger.info("小程序 addMpUser {}",JSON.toJSONString(omMpuser));
        OmMpuser omMpuser1_db = omMpuserService.selectOmMpuserByOpenId(omMpuser.getMpuserOpenid());
        if(omMpuser1_db != null){
            return error("此用户已经存在。避免藏数据 不允许插入");
        }
        if(omMpuserService.insertOmMpuser(omMpuser) > 0){
            //添加成功
            OmMpuser omMpuser1 = omMpuserService.selectOmMpuserByOpenId(omMpuser.getMpuserOpenid());
            return success(omMpuser1);
        }
        return error("addMpUser 失败");
    }

    @PostMapping("/editMpUser")
    public AjaxResult editMpUser(@RequestBody OmMpuser omMpuser){
        logger.info("小程序 editMpUser {}",JSON.toJSONString(omMpuser));
        logger.info("当前登录的小程序用户uid- {}",omMpuser.getMpuserId());
        return toAjax(omMpuserService.updateOmMpuser(omMpuser));
    }

    @PostMapping("/infoMpUserByopenid")
    public AjaxResult infoMpUserByopenid(@RequestBody OmMpuser omMpuser){
        logger.info("小程序 infoMpUser {}",JSON.toJSONString(omMpuser));
        logger.info("当前登录的小程序用户openid- {}",omMpuser.getMpuserOpenid());

        //return toAjax(omMpuserService.selectOmMpuserByOpenId(omMpuser.getMpuserOpenid()));

        return success(omMpuserService.selectOmMpuserByOpenId(omMpuser.getMpuserOpenid()));
    }


    @PostMapping("/infoMpUserByuid")
    public AjaxResult infoMpUser(@RequestBody OmMpuser omMpuser){
        logger.info("小程序 infoMpUser {}",JSON.toJSONString(omMpuser));
        logger.info("当前登录的小程序用户openid- {}",omMpuser.getMpuserOpenid());

        //return toAjax(omMpuserService.selectOmMpuserByOpenId(omMpuser.getMpuserOpenid()));

        return success(omMpuserService.selectOmMpuserByMpuserId(omMpuser.getMpuserId()));
    }



}
